﻿using System;
using System.Collections.Generic;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.Result;

namespace NTTDATA.BEENChina.ResultCertification.Logic
{
    public class CheckEntryResult
    {
      
        private  IDataSource dataSourceAccess;

        public CheckEntryResult()
        {
            dataSourceAccess = new  DataSource();
        }


        public IEnumerable<string> CheckCategorys { get; set; }

        /// <summary>
        /// 得到子画像的录入结果
        /// </summary>
        /// <param name="serverCode"></param>
        /// <param name="orderId"></param>
        /// <param name="searchCollections">批量查询 </param>
        /// <returns></returns>
        public List<ChildImage> GetEntryResult(string serverCode,string orderId,bool searchCollections = false)
        {
            try
            {

           
                if (CheckCategorys != null && CheckCategorys.Any())
                {
                    string stoeTableName = "store_" + serverCode; 
                           
  
                    //得到store表里面所有的CategoryID
                    List<string> categorys = dataSourceAccess.GetCategoryIdsFromChildImage(stoeTableName, orderId);

                    if (categorys != null)
                    {
                        //2013-11-18 add AsParallel()
                        List<string> finalCategorys = CheckCategorys.AsParallel().Where(categorys.Contains).ToList();

                        List<ChildImage> childImages = new List<ChildImage>();


                        if (searchCollections)
                        {
                            if (finalCategorys.Count > 0)
                            {
                                var images = dataSourceAccess.GetChildImageObjects(stoeTableName, orderId, finalCategorys);


                                if (images != null && images.Any())
                                    childImages.AddRange(images);
                            }

                        }
                        else
                        {
                            if (finalCategorys.Count > 0)
                            {
                                foreach (var categoryId in finalCategorys)
                                {
                                    var images = dataSourceAccess.GetChildImageObjects(stoeTableName, orderId, categoryId);

                                    if (images != null && images.Any())
                                        childImages.AddRange(images);
                                }
                            }
                        }

                        return childImages;
                    }
                }

          

            }
            catch (Exception ex)
            {

                Log.Console.Error("CheckEntryResult GetEntryResult", ex);

            }  
            
            return null;
        }

     
    }
}
